<template>
  <div>
    <el-divider></el-divider>
    <!-- 表单组件 -->
    <el-form
      class="login"
      ref="form"
      :model="form"
      label-width="100px"
      style="width: 500px"
      :rules="rules"
    >
      <el-form-item label="账号" prop="name" ref="nameInput">
        <el-input v-model="form.name" placeholder=""></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="pwd">
        <el-input
          type="password"
          show-password
          v-model="form.pwd"
          placeholder=""
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: '',
        pwd: '',
        phone: '',
      },
      rules: {
        name: [
          { required: true, message: '账号必填', trigger: 'blur' },
          { min: 5, max: 15, message: '字符长度[5-15]', trigger: 'blur' },
        ],
        pwd: [
          { required: true, message: '密码必填', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '密码需要6位数字', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    submit() {
      let form = this.$refs['form']
      console.log(form)
      let nameInput = this.$refs.nameInput
      console.log(nameInput)

      // validate方法用于验证整个表单
      // 验证成功返回TRUE
      // 验证失败返回FALSE
      form.validate(valid => {
        if (valid) {
          console.log('用户点击了提交按钮', this.form)
        } else {
          console.log('验证失败')
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.login {
  margin: 200px auto;
}
</style>
